<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录导航栏 -->
    <!-- 登录表单数据 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <!-- 用户名 -->
      <van-field
        type="number"
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userformRules.mobile"
        maxlength="11"
      >
        <i slot="left-icon" class="headLine headLine-shouji"></i>
      </van-field>
      <!-- 验证码 -->
      <van-field
        v-model="user.code"
        type="number"
        maxlength="6"
        name="code"
        center
        clearable
        placeholder="请输入短信验证码"
        :rules="userformRules.code"
      >
        <i slot="left-icon" class="headLine headLine-yanzhengma"></i>
        <template #button>
          <van-count-down
            v-if="isShowTime"
            :time="1000 * 60"
            format="ss s"
            @finish="isShowTime = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="default"
            @click.stop="sendCode"
            native-type="button"
            >发送验证码
          </van-button>
        </template>
      </van-field>
      <div :class="{ submit: !(user.mobile && user.code) }">
        <div class="login-btn-wrap">
          <van-button block type="info" native-type="submit">登录</van-button>
        </div>
      </div>
    </van-form>
  </div>
</template>

<script>
import { userInfoApi, userCodeApi } from '@/api/index.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      isShowTime: false,
      user: {
        mobile: '18181075781',
        code: '246810'
      },
      userformRules: {
        // 手机校验
        mobile: [
          { required: true },
          { pattern: /^1[3,5,7,8,9]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true },
          { pattern: /^\d{6}$/, message: '验证码错误' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit(val) {
      // 在组件中必须通过this.$toast
      // 加载持续时间
      // 小弹窗
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true,
        duration: 0
      })
      // 2 请求 接口 提交数据 登录状态
      try {
        const { data } = await userInfoApi(val.mobile, val.code)
        this.$toast.success('登录成功!')
        // 登录成功后 保存 token 的数据
        this.$store.commit('saveUserInfo', data.data)
        this.$router.push('/my')
        console.log(this.$store.status)
      } catch (err) {
        console.log(err.response.status)
        if (err.response.status === 400) {
          console.log('手机号或者验证码错误!')
          this.$toast.fail('手机号或者验证码错误!')
        } else {
          console.log('出现其他问题，请稍后再试!')
          this.$toast.fail('登录失败!')
        }
      }
    },
    // 发送验证码
    async sendCode() {
      // 验证 即将发送短信的手机号是否正确
      try {
        await this.$refs.loginForm.validate('mobile')
        this.isShowTime = true
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 3 验证通过登录 验证码注册
      try {
        const res = await userCodeApi(this.user.mobile)
        console.log('短信发送成功', res)
      } catch (err) {
        this.isShowTime = false
        if (err.response.status === 429) {
          this.$toast('发送过于频繁!稍后再试!')
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .headLine {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 160px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
